<?php
namespace app\admin\controller\auth;

use app\admin\model\AuthGroup;
use app\common\controller\Backend;
use fast\Tree;
use think\Cache;
use fast\Random;
use fast\Cols;
use fast\Date;
/**
 * 管理员日志
 *
 * @icon fa fa-users
 * @remark 管理员可以查看自己所拥有的权限的管理员日志
 */
class Adminlog extends Backend
{
    protected $model = null;
    protected $rulelist = [];
    protected $multiFields = 'ismenu,status';
    protected $childrenGroupIds = [];
    protected $childrenAdminIds = [];

    public function initialize()
    {
        parent::initialize();
        $this->model = model('AdminLog');

        $this->childrenAdminIds = $this->auth->getChildrenAdminIds(true);
        $this->childrenGroupIds = $this->auth->getChildrenGroupIds($this->auth->isSuperAdmin() ? true : false);

        $groupName = AuthGroup::where('itemid', 'in', $this->childrenGroupIds)
                ->column('itemid,name');

        $this->view->assign('groupdata', $groupName);
    }

    /**
     * 查看
     */
    public function index(){
        $columnlist = Cols::columnread('adminlog');
        $flag = array();$options = array();
        foreach($columnlist as $v){
            $flag[] = $v['listorder'];
            if($v['options']&&$v['isshowlist']==1){
                //把带有选项的列，提取出来。
                $options[$v['columnenname']] = $v['options'];
            }
        }
        array_multisort($flag, SORT_ASC, $columnlist);
        //设置过滤方法
        $this->request->filter(['strip_tags']);
        //如果发送的来源是Selectpage，则转发到Selectpage
        if ($this->request->request('pkey_name')){
            return $this->selectpage();
        }
        $searchfileds = array();
        $searchdefault = array();
        $searchfileds['username'] = 'varcharshort';
        list($where, $sort, $order, $offset, $limit) = $this->buildparams($searchfileds);
        $limit = 300;
        $order = 'desc';
        $sort = 'createtime';
        $total = $this->model->where($where)->order($sort, $order)->count();
        $totalpage = 1;$page = 1;
        if($limit>0){
            $totalpage = $total%$limit==0?intval($total/$limit):intval($total/$limit)+1;
            //总页数
            if($offset>0){$page = intval($offset/$limit)+1;}
        }
        //print_r($where);exit;
        $list = $this->model
                ->where($where)
                ->order($sort, $order)
                ->limit($offset, $limit)
                ->select();

        for ($i = 0; $i < count($list); $i++) {
            if(isset($list[$i]['category'])){
                $list[$i]['catname'] = $list[$i]['category']['name'];
            }
            unset($list[$i]['intro']);unset($list[$i]['content']);
            foreach ($options as $k => $v) {
                if(isset($list[$i][$k])){
                    $arj = json_decode($v,true);
                    $sarj = $arj[$list[$i][$k]];
                    $list[$i][$k] = $sarj;
                }
            }
            //$list[$i]['logintime'] = Date::timetodate($list[$i]['logintime'],3);
            //$list[$i]['createtime'] = Date::timetodate($list[$i]['createtime'],3);
            //$list[$i]['updatetime'] = Date::timetodate($list[$i]['updatetime'],3);
        }

        $result = array("total" => $total, "rows" => $list);
        $this->view->assign("lists",$result['rows']);
        $this->view->assign("listsjson",json_encode($result['rows']));
        $this->view->assign("columnlistjson",json_encode($columnlist));
        $this->view->assign("columnlist",$columnlist);
        $this->view->assign("total",$result['total']);
        $this->view->assign("offset",$offset);
        $this->view->assign("limit",$limit);
        $this->view->assign("totalpage",$totalpage);
        $this->view->assign("page",$page);
        return $this->view->fetch('index');
    }

    /**
     * 详情
     */
    public function detail($ids)
    {
        $row = $this->model->get(['id' => $ids]);
        if (!$row)
            $this->error(__('No Results were found'));
        $this->view->assign("row", $row->toArray());
        return $this->view->fetch();
    }

    /**
     * 添加
     * @internal
     */
    public function add()
    {
        $this->error();
    }

    /**
     * 编辑
     * @internal
     */
    public function edit($ids = NULL)
    {
        $this->error();
    }

    /**
     * 删除
     */
    public function del($ids = "")
    {
        if ($ids)
        {
            $childrenGroupIds = $this->childrenGroupIds;
            $adminList = $this->model->where('id', 'in', $ids)->where('admin_id', 'in', function($query) use($childrenGroupIds) {
                        $query->name('auth_group_access')->field('uid');
                    })->select();
            if ($adminList)
            {
                $deleteIds = [];
                foreach ($adminList as $k => $v)
                {
                    $deleteIds[] = $v->id;
                }
                if ($deleteIds)
                {
                    $this->model->destroy($deleteIds);
                    $this->success();
                }
            }
        }
        $this->error();
    }

    /**
     * 批量更新
     * @internal
     */
    public function multi($ids = "")
    {
        // 管理员禁止批量操作
        $this->error();
    }

    public function selectpage()
    {
        return parent::selectpage();
    }

}
